package com.twu.biblioteca;

import java.util.Scanner;

    public class Librarian {

    BookRepository bookRepository = new BookRepository();
    MovieRepository movieRepository = new MovieRepository();
    Scanner input = new Scanner(System.in);
    User loggedInUser;
    Menu menu = new Menu();

    public boolean verifyLogin(String username, String password){
        UserRepository userRepository = new UserRepository();
        for(User user:userRepository.userList)
        {
            if(username.equalsIgnoreCase(user.username) && (password.equalsIgnoreCase(user.password)))
                {
                  System.out.println("Success!");
                  loggedInUser = user;
                  return true;
                }
        }
        System.out.println("Invalid Credentials.");
    return false;
    }

    public void displayBooks(){
        System.out.println(bookRepository.bookDisplay());
        }

    public void displayMovies(){
        System.out.println(movieRepository.movieDisplay());
    }


    public void bookCheckOutOperation(String title){
        if(bookRepository.checkOutBook(title)){
            System.out.println("You may have the book. Have a nice time reading it!");
        }
        else System.out.println("Sorry, such a book does not exist.");
    }

    public void movieCheckOutOperation(String title){
        if(movieRepository.checkOutMovie(title)){
            System.out.println("You may have the movie. Have a nice time watching it!");
        }
        else System.out.println("Sorry, such a movie does not exist.");
    }

    public void returnBookOperation(String title){
        if(bookRepository.returnBook(title)){
            System.out.println("The book has been returned. Thank you.");
        }
        else System.out.println("Sorry. That's an invalid book.");
    }

    public void returnMovieOperation(String title){
        if(movieRepository.returnMovie(title)){
            System.out.println("The movie has been returned. Thank you.");
        }
        else System.out.println("Sorry. That's an invalid movie.");
    }


    public boolean processUserPreLogin(int choice){
            switch (choice){
                case 1:
                    System.out.println(bookRepository.bookDisplay());
                    break;
                case 2:
                    System.out.println(movieRepository.movieDisplay());
                    break;
                case 3:
                    System.out.println("Enter username:");
                    String username = input.next();
                    System.out.println("Enter password");
                    String password = input.next();
                    return (verifyLogin(username,password));

                case 4:
                    System.exit(0);
                default:
                    System.out.println("Invalid Input.");
            }
                    return false;
}
    public boolean processUserPostLogin(int choice){

             switch (choice){
                 case 1:
                     displayBooks();
                     break;
                 case 2:
                    displayMovies();
                     break;
                case 3:
                    System.out.println("Enter the book title to be checked out");
                    String bookTitle = input.next();
                    bookCheckOutOperation(bookTitle);
                    break;
                 case 4:
                    System.out.println("Enter the movie title to be checked out");
                    String movieTitle = input.next();
                    movieCheckOutOperation(movieTitle);
                    break;
                 case 5:
                    System.out.println("Which book do you have to return?\n");
                    String bookToReturn = input.next();
                    returnBookOperation(bookToReturn);
                    break;

                 case 6:
                     System.out.println("Which movie do you have to return?\n");
                     String movieToReturn = input.next();
                     returnBookOperation(movieToReturn);
                     break;

                 case 7:
                    System.out.println("\nAbout User\n");
                    System.out.println(displayDetailsOfUser(loggedInUser));
                    break;
                 case 8:
                    menu.displayOptionsBeforeLogin();
                    return false;
                 case 9:
                    System.exit(0);
                 default:
                    System.out.println("Shame. Check option.");

            }
                    return true;
 }

    public String displayDetailsOfUser(User user)

        {
            return ("\n Name = "+ user.name + "\n User name = " + user.username + "\n User email = " + user.email + "\n Phone number = " + user.phone + "\n Password = " + user.password);
        }


}
